package sort;

import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort2 {


    private static void random(int[] arr) {
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(50);
        }
    }

    // 插入排序
    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            // 抽牌
            int tmp = arr[i];
            int j = i - 1;
            for(; j >= 0; j--) {
                if (tmp < arr[j]) {
                    arr[j + 1] = arr[j];
                }else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }
    }

    // 希尔排序
    private static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            shell(arr, gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (tmp < arr[j]) {
                    arr[j + gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j + gap] = tmp;
        }
    }

    private static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minValIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minValIndex]) {
                    minValIndex = j;
                }
            }
            swap(arr, i, minValIndex);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    private static void heapSort(int[] arr) {
        createHeap(arr);

        int size = arr.length;
        while (size != 0) {
            swap(arr, 0, size-1);
            size--;
            downward(arr,0,size);
        }
    }

    private static void createHeap(int[] arr) {
        int size = arr.length;
        for (int parent = (size - 1 - 1) / 2; parent >= 0; parent--) {
            downward(arr, parent, size);
        }
    }

    private static void downward(int[] arr, int parent, int size) {
        int child = 2 * parent + 1;
        while (child < size) {
            if (child + 1 < size && arr[child] < arr[child + 1]) {
                child = child + 1;
            }

            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }




    public static void bubbleSort(int[] arr) {
        int length = arr.length;
        // i 轮次
        boolean flag = false;
        for (int i = 0; i < length - 1; i++) {
            for (int j = 0; j < length - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr, j, j+1);
                    flag = true;
                }
            }
            if (!flag) {
                // flag为false说明没改, 该序列已有序, 退出循环
                break;
            }
        }
    }




    private static void hoare(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int pivotIndex = new Random().nextInt((right - left + 1)) + left;
        swap(arr, left, pivotIndex);
        int pivot = hoarePartition(arr, left, right);


        hoare(arr, left, pivot - 1);
        hoare(arr, pivot + 1, right);
    }

    private static int hoarePartition(int[] arr, int left, int right) {
        int pivot = arr[left];
        int pivotIndex = left;
        while (left < right) {
            while (left < right && arr[right] >= pivot) {
                right--;
            }
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            swap(arr, left, right);
        }
        swap(arr,pivotIndex,left);
        return left;
    }

    public static void quickSortNor(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        Stack<Integer> stack = new Stack<>();
        int pivot = hoarePartition(arr, left, right);
        if (pivot - 1 > left) {
            stack.push(left);
            stack.push(pivot - 1);
        }
        if (pivot + 1 < right) {
            stack.push(pivot + 1);
            stack.push(right);
        }

        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = hoarePartition(arr, left, right);
            if (pivot - 1 > left) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot + 1 < right) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }

    private static void pointerQuick(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int pivotIndex = new Random().nextInt(right - left + 1) + left;
        swap(arr, left, pivotIndex);
        int pivot = pointerPartition(arr, left, right);
        pointerQuick(arr, left, pivot - 1);
        pointerQuick(arr, pivot + 1, right);
    }

    private static int pointerPartition(int[] arr, int left, int right) {
        int prev = left;
        int cur = prev + 1;
        while (cur <= right) {
            if (arr[cur] <= arr[left] && arr[++prev] != arr[cur]) {
                swap(arr, prev, cur);
            }
            cur++;
        }
        swap(arr, left, prev);
        return prev;
    }

    public static void quickSort(int[] arr) {
        pointerQuick(arr, 0, arr.length - 1);
    }


    private static void mergeSortNor(int[] arr) {
        int gap = 1;
        int n = arr.length;
        while (gap < n) {
            for (int i = 0; i < n; i += gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;

                if (mid >= n) {
                    mid = n - 1;
                }
                if (right >= n) {
                    right = n - 1;
                }

                merge(arr, left, mid, right);
            }
            gap *= 2;
        }
    }

    public static void mergeSort(int[] arr) {
        mergeSortFun(arr, 0, arr.length - 1);
    }

    private static void mergeSortFun(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = left + ((right - left) >> 1);
        mergeSortFun(arr, left, mid);
        mergeSortFun(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int left1 = left;
        int right1 = mid;
        int left2 = mid + 1;
        int right2 = right;

        int[] tmp = new int[right - left + 1];
        int i = 0;
        while (left1 <= right1 && left2 <= right2) {
            if (arr[left1] < arr[left2]) {
                tmp[i++] = arr[left1++];
            }else {
                tmp[i++] = arr[left2++];
            }
        }

        while (left1 <= right1) {
            tmp[i++] = arr[left1++];
        }

        while (left2 <= right2) {
            tmp[i++] = arr[left2++];
        }

        for (int j = 0; j < tmp.length; j++) {
            arr[j + left] = tmp[j];
        }
    }



    public static void main(String[] args) {
        int[] arr = new int[10];
        random(arr);
        System.out.println("原数组:" + Arrays.toString(arr));

        countSort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
    }

    private static void countSort(int[] arr) {
        int minVal = arr[0];
        int maxVal = arr[0];

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < minVal) {
                minVal = arr[i];
            }
            if (arr[i] > maxVal) {
                maxVal = arr[i];
            }
        }

        int length = maxVal - minVal + 1;
        int[] count = new int[length];

        for (int i = 0; i < arr.length; i++) {
            count[arr[i] - minVal]++;
        }

        int index = 0;
        for (int i = 0; i < length; i++) {
            while (count[i] != 0) {
                arr[index++] = i + minVal;
                count[i]--;
            }
        }
    }
}
